package com.cinema.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinema.bean.Cinema;
import com.cinema.bean.Role;
import com.cinema.bean.User;
import com.cinema.bean.User;
import com.cinema.service.RedisService;
import com.cinema.service.RoleService;
import com.cinema.service.UserService;
import com.cinema.util.DeCodeUtil;
import com.cinema.util.JWTUtil;
import com.cinema.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")

@CrossOrigin
public class UserController {

    @Autowired //依赖注入
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    RedisService redisService;

//    @RequestMapping("list")
//    public List<User> getList(){
//        List<User> list=userService.list();
//        return list;
//    }

    @GetMapping("list")
    public Result getList(){
        List<User> list= userService.list();
        for (User item:list){
            Role role=roleService.getById(item.getRoleId());
            item.setRole(role);
        }
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam("searchText") String searchText){
        System.out.println(searchText);
        IPage<User> result=null;
        if(searchText==null||searchText.trim().isEmpty()){
            result=userService.page(page);
        }else {
            QueryWrapper wrapper=new QueryWrapper();

            wrapper.like("user_name",searchText);
            wrapper.or();
            wrapper.like("phone",searchText);
            result=userService.page(page,wrapper);
        }
        return Result.success(result);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        user.setPassword("123123");
        String pass=user.getPassword();
        String newpass=DeCodeUtil.decode(pass);
        user.setPassword(newpass);

        userService.save(user);
        User tuser=userService.getById(user.getUserId());
        return Result.success("保存成功",user);

    }
    @DeleteMapping("/{userId}")
    public Result delete(@PathVariable("userId") Integer userId){
        boolean b=userService.removeById(userId);
        if(b){
            return Result.success("删除成功",null);
        }
        else{
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody User user){
        boolean b=userService.saveOrUpdate(user);
        User tuser=userService.getById(user.getUserId());
        if(b){
            return Result.success("保存成功");
        }
        else {
            return Result.fail(201,"修改失败");
        }
    }
    @PostMapping("login")
    public Result login(User user, @RequestParam("captcha") String captcha,@RequestParam("captchakey") String captchakey ) {
        String captchacheck=checkCaptcha(captchakey,captcha);
        if(!captchacheck.equals("success")){
            return Result.fail(402,captchacheck);
        }
        User user1 = userService.selectByName(user.getPhone());
        if (user1 != null) {
            boolean check = DeCodeUtil.decode(user.getPassword()).equals(user1.getPassword());
            if (check) {
                String token = JWTUtil.getToken();
                return Result.success("登陆成功", token);
            }
        }
        return Result.fail(401,"认证失败");
    }

    private String checkCaptcha(String captchakey, String captchaCode ) {
        if(captchakey==null){
            return "请求参数异常";
        }
        String code=(String)redisService.get(captchakey);
        if(code==null){
            return "验证码已过期";
        }
        if(captchaCode.equalsIgnoreCase(code)) {//忽略大小写
            return "success";
        }
        return "验证失败";
    }
}
